classdef ThesisHelper< handle
    properties (Constant)
        figSize = [300 300 700 400];
    end
    methods(Static = true)
%         function bandX = loadBands(bandFile, headB, tailB)
%             bandX = load(bandFile, '-ascii');
%             
%             bandX = first+1 : end - last, :);
%         end
        
        %%XX,204*24
        function plot24Curve(XX, headB, tailB, bandX)     
%             XX = XX*100; %%转成100%
            lenX = size(XX, 2);
            fig = figure;
            hold on
            bands = size(XX, 1);
            xxxx = linspace(935.6100, 1720.2300, bands);
            if nargin >3
                xxxx = bandX;%%2024-02-07            
            end
            
            for i = 1 : lenX
%                 plot(XX(:, i), 'Color', colors(i, :));
                plot(xxxx, XX(:, i));
            end
%             legend('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', ...
%                    '13', '14', '15', '16', '17', '18', '19', '8', '9', '10', '11', '12')
            
            xlim([930, 1721])
%             ylim([0.25, 0.75])
%             ylim([25, 75])
            
            ymin = min(XX,[],'all') *.8;
            ymax = max(XX,[],'all') *1.02;
            kk = xxxx(headB+1);
            plot([kk, kk], [ymin, ymax], 'k--')
            kk = xxxx(bands - tailB);
            plot([kk, kk], [ymin, ymax], 'k--')
%             set (gca,'position',[0.1,0.1,0.9,0.9] );
            box on
            hold off
            xlabel('Wavelength(nm)')
%             ylabel('Reflectance(%)')
            ylabel('Reflectance')
            
            f = ThesisHelper.GetAFrame();
            im = frame2im(f);
            saveas(gca, 'F:\12份土壤\sun\thesis\223条光谱曲线.emf')
            savefig('F:\12份土壤\sun\thesis\223条光谱曲线.fig')
        end
        
        function f = GetAFrame()%%截图时，增加坐标轴
            set(gcf,'Position', ThesisHelper.figSize);
            ax = gca;
            ax.Units = 'pixels';
            pos = ax.Position;
            marg = 30;
            rect = [-marg, -marg, pos(3)+2*marg,pos(4)+2*marg];
            f = getframe(gca,rect);
            
            % %             set (gca,'position',[0,0,1,1])

            % %             saveas(gca, fileSave, 'png');
        end
        
        %%XX,204*24
        function plot24CurveNoHT(XX, saveName, bandX)     
%             XX = XX*100; %%转成100%
            lenX = size(XX, 2);
            fig = figure;
            hold on
            bands = size(XX, 1);
            xxxx = linspace(956.3500, 1677.2900, bands);
            if nargin >2
                xxxx = bandX;%%2024-02-07            
            end
            
            for i = 1 : lenX
%                 plot(XX(:, i), 'Color', colors(i, :));
                plot(xxxx, XX(:, i));
            end
%             legend('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', ...
%                    '13', '14', '15', '16', '17', '18', '19', '8', '9', '10', '11', '12')
            
            xlim([955, 1678])
%             ylim([0.25, 0.75])
%             ylim([30, 70])
            
            box on
            hold off
            xlabel('Wavelength(nm)')
%             ylabel('Reflectance(%)')
            ylabel('Reflectance')
            
            f = ThesisHelper.GetAFrame();
            im = frame2im(f);
            saveas(gca, saveName)
        end
        
        
        function SaveDataToFormatFile(fileIn, fileSave)

            fileID = fopen(fileIn);
            data = textscan(fileID, '%f');
            fclose(fileID);

            data = reshape(data{1}, [36, 4])';
            data = data([4,1], :);
            data = reshape(data, [12, 6]);
            
            fileID = fopen(fileSave, 'w');
            fprintf(fileID, '%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\r\n', data);
            fclose(fileID);            
        end
        
        
        function DrawMetalsFigure(data, fileSave)
            
            xlabels = {'SG\_PLSR', 'SG\_SVR', 'SG\_RF', 'SG\_LSB'...
                       'MSC\_PLSR', 'MSC\_SVR', 'MSC\_RF', 'MSC\_LSB'...
                       'SNV\_PLSR', 'SNV\_SVR', 'SNV\_RF', 'SNV\_LSB'...
                       'CR\_PLSR', 'CR\_SVR', 'CR\_RF', 'CR\_LSB'...
                       'FD\_PLSR', 'FD\_SVR', 'FD\_RF', 'FD\_LSB'};
            
            yylim = [-0.2, 1.6;     0, 2;
                      -0.2, 2.2;    -0.2, 1.6;      
                     0, 4.5;       0, 4];  
             
            for k = 1 : 6
                figure
                hold on
                dd = data(:, k +6: 6 : end);
                
                ee = dd(4:4:end, :);
                
                yyaxis left
                plot(1:20, ee(:), '-o')       
                xticks(1:20)
                xticklabels(xlabels)
                xtickangle(45)
                xlim([0.6, 20.2])
                
                if k == 1
                    yticks(0.0 : 0.2 : 1.6)                    
                end
                if k == 4
                    yticks(0.0 : 0.2 : 1.6)                    
                end
                ylim(yylim(k, :))
                ylabel('Residual prediction deviation');
                
                yyaxis right
                ff = dd(1:4:end, :);
                plot(1:20, ff(:), '--s')                
                ylabel('Root square error');
                
                ylim([0, 50])
                if k == 5
                    ylim([0, 30])
                end
                if k == 2
                    yticks(0:3)    
                    ylim([0, 3])
                end
                
                legend('RPD','RMSE')
                hold off
                
                f = ThesisHelper.GetAFrame();
                im = frame2im(f);
                saveas(gca, fileSave{k})
            end
            
        end
        
        function ret = ExpandOneMetric(data)
            metal = cell(3, 1);
            for i = 1:3
                metal{i} = data(:, i:3:end);
                metal{i} = metal{i}';
                metal{i} = metal{i}(:);                 
            end
            ret = [metal{1}; metal{2}; metal{3}];
        end
        
        function ret = DrawMetricFigures(data, fileSave)
            insr = [1:10, 21:30, 41:60];
%             insC = [7:30];
            data = data(insr, :);
 
            r2  = data(7:10:end, :);
            rmse = data(6:10:end, :);
            mape = data(10:10:end, :);         
            
            metal = cell(3, 1);
            for i = 1 : 3
                metal{i} = r2(:, i:3:end);
                metal{i} = metal{i}';
                metal{i} = metal{i}(:);                 
            end
            r2 = ThesisHelper.ExpandOneMetric(r2);
            rmse = ThesisHelper.ExpandOneMetric(rmse);
            mape = ThesisHelper.ExpandOneMetric(mape);
            
            ret = [r2, rmse, mape]';           
           
            
            fileID = fopen(fileSave, 'w');
            fprintf(fileID, '%.3f\t%.3f\t%.3f\r\n', ret);
            fclose(fileID);          
        end
        
        %%针对 预处理+模型
        function DrawMetricTables(data, fileSave)            
%             data = data(6:12:end, :);
            AN = data(:, 1:3:end);
            AP = data(:, 2:3:end);
            AK = data(:, 3:3:end);
            
            r2   = data(7 :10:end, :);
            rpd  = data(9 :10:end, :);
            rmse = data(6 :10:end, :);
            mape = data(10:10:end, :);    
            
%             dataAN = mat2cell(AN, [12,12,12,12,12,12,12], [1,1,1,1,1,1,1]);
%             dataAP = mat2cell(AP, [12,12,12,12,12,12,12], [1,1,1,1,1,1,1]);
%             dataAK = mat2cell(AK, [12,12,12,12,12,12,12], [1,1,1,1,1,1,1]);
%             dataAN = cell2mat(dataAN');
%             dataAP = cell2mat(dataAP');
%             dataAK = cell2mat(dataAK');
%             
%             r2c = dataAN            
         
            nutrient = cell(3, 1);           
            
            r2 = r2';
            rmse = rmse';
            mape = mape';
            for k = 1 : 3 %%3种元素
                rr2 = r2(k:3:end, :);
                rrmse = rmse(k:3:end, :);
                rmape = mape(k:3:end, :);
                for i = 1 : 7%%
                    if i == 1
                        nutrient{k} = [rr2(i, :); rrmse(i, :); rmape(i,:)];
                    else                        
                        nutrient{k} = [nutrient{k}; rr2(i, :); rrmse(i, :); rmape(i,:)];
                    end
                end
            end
            
%             
            for i = 1:3
                fileID = fopen(fileSave{i}, 'w');                
                ddd = nutrient{i};         
                %%2025-07-07 原来的6个预处理+Full
                                 
                %%2025-07-07 只留4个预处理+Full 
% %                 ddd = ddd([1:9, 13:18], [1,3,5,6]);%%行列反转   （FULL，SG，MSC，MA， FD)           
                ddd = ddd([1:12, 16:18], [1,3,5,6]);%%行列反转   （FULL，SG，MSC，SNV，FD) FD)
% %                 fprintf(fileID, '%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\r\n', ddd');
                fprintf(fileID, '%.3f\t%.3f\t%.3f\t%.3f\r\n', ddd');
                fclose(fileID);     
            end
        end
        
        %% 针对 特征选择+预处理  data(特征选择*预处理)
        %% 针对 特征选择+预处理  data(特征选择*预处理)
        function BuildMetricTables(data, fileSave)
%             insr = [11:30, 41:60];
            insC = [1:12, 16:18];
            data = data(1:end, insC);
            
            r2  = data(7:10:end, :)';
            rmse = data(6:10:end, :)';
            mape = data(10:10:end, :)';       
            
            metal = cell(3, 1);           
            for i = 1 : length(metal)
                ppp = r2(i:3:end, :);
                mmm = rmse(i:3:end, :);
                aaa = mape(i:3:end, :);
                
                ttt = [ppp; mmm; aaa];
                ddd = zeros(size(ttt));%%ddd 转成（预处理*特征选择）
                ddd(1:3:end, :) = ppp;
                ddd(2:3:end, :) = mmm;
                ddd(3:3:end, :) = aaa;
                metal{i} = ddd;
            end
            
            for i = 1 : length(metal)
                fileID = fopen(fileSave{i}, 'w');                
                ddd = metal{i};
                ddd = ddd'; %% 保存要转置
                bbb = repmat('%.3f\t', 1, 7);
                fprintf(fileID, [bbb, '\n'], ddd);
                fclose(fileID);     
            end
            
        end
        function BuildMetricTables2(data, fileSave)
            insr = [1:10, 21:30, 41:60];
%             insC = [7:30];
            data = data(insr, :);
            
            r2  = data(7:10:end, :);
            rmse = data(6:10:end, :);
            mape = data(10:10:end, :);       
            
            metal = cell(3, 1);           
            for i = 1 : length(metal)
                ppp = r2(:, i:3:end);
                mmm = rmse(:, i:3:end);
                aaa = mape(:, i:3:end);
                
                ttt = [ppp', mmm', aaa'];
                ddd = zeros(size(ttt));
                ddd(:, 1:3:end) = ppp';
                ddd(:, 2:3:end) = mmm';
                ddd(:, 3:3:end) = aaa';
                metal{i} = ddd;
            end
            
            for i = 1 : length(metal)
                fileID = fopen(fileSave{i}, 'w');                
                ddd = metal{i};
                ddd = ddd';
                fprintf(fileID, '%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\r\n', ddd);
                fclose(fileID);     
            end
            
        end
 
        
        function GetFeatureData(data, fileSave)
            
            rpd = data(4: 5: end, :);
            rmse = data(1: 5: end, :);
            mape = data(5: 5: end, :);
          
            ddd = zeros(size(rpd, 1) *3, size(rpd, 2));
            
            ddd(1:3:end, :) = rpd;
            ddd(2:3:end, :) = rmse;
            ddd(3:3:end, :) = mape;
            
            ddd = ddd';
            fileID = fopen(fileSave, 'w');   
%             fprintf(fileID, '%.3f\t%.3f\t%.3f\t%.3f\r\n', ddd);
            fprintf(fileID, '%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\r\n', ddd);
            fclose(fileID);     
            
        end
        
        function DrawBarFigure(fileLoad, fileSavePrefix, fileDiff, isClose)
            
            szFig = [200, 200, 800, 200];
            for i = 1:3
                metal = load(fileLoad{i}, '-ascii');%对应不同养分的数据
                
                r2 = metal(1 : 3 : end, :)';                
                rmse = metal(2 : 3 : end, :)';                
                mape = metal(3 : 3 : end, :)';                
                
%                 lll = {'Full', 'SG', 'MSC', 'MA', 'FD'};
                lll = {'Full', 'SG', 'MSC', 'SNV', 'FD'};
                
                fig{1} = figure('Position', szFig);
                bar(r2);
%                 xticklabels({'PLSR','SVR','RF','LSBoost'});
                xticklabels({'PLSR','SVR','RF','ANN'});
                ylabel('R2')
                ylim([0, 0.8])
                legend(lll, 'NumColumns',3);
                
                fig{2} = figure('Position', szFig);
                bar(rmse);
%                 xticklabels({'PLSR','SVR','RF','LSBoost'})
                xticklabels({'PLSR','SVR','RF','ANN'});
                legend(lll, 'NumColumns',3);
                ylim([0, 80])
                ylabel('RMSE')
                if i == 1
                    ylim([0, 40])
                elseif i ==2
                    ylim([0, 50])
                end
                    
                
                fig{3} = figure('Position', szFig);
                bar(mape);
%                 xticklabels({'PLSR','SVR','RF','LSBoost'})
                xticklabels({'PLSR','SVR','RF','ANN'});
                legend(lll, 'NumColumns',3);
                ylabel('MAPE')         
                if i == 1
                    ylim([0, 0.6])
                elseif i ==2
                    ylim([0, 0.7])
                else
                    ylim([0, 0.25])
                end
                
                
                for k = 1 : 3
                    fileSave = [fileSavePrefix{i}, fileDiff{k}];
                    figure(fig{k})
                    
% %                     f = ThesisHelper.GetAFrame(szFig);
% %                     im = frame2im(f);
                    saveas(gca, fileSave)    
                    if isClose
                        close(fig{k})
                    end
                end
            end
            
        end
   
    end
end